
// bdc2Doc.h : interface of the Cbdc2Doc class
//


#pragma once

#include "Cbdc2Dialog.h"
#include "bdc2Def.h"
using namespace bdc;


class Cbdc2Doc : public CDocument
{
protected: // create from serialization only
	Cbdc2Doc();
	DECLARE_DYNCREATE(Cbdc2Doc)

// Attributes
public:

// Operations
public:

// Overrides
public:
	virtual BOOL OnNewDocument();
	virtual void Serialize(CArchive& ar);
#ifdef SHARED_HANDLERS
	virtual void InitializeSearchContent();
	virtual void OnDrawThumbnail(CDC& dc, LPRECT lprcBounds);
#endif // SHARED_HANDLERS

// Implementation
public:
	virtual ~Cbdc2Doc();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	DECLARE_MESSAGE_MAP()

	// Generated OLE dispatch map functions

	DECLARE_DISPATCH_MAP()
	DECLARE_INTERFACE_MAP()

#ifdef SHARED_HANDLERS
	// Helper function that sets search content for a Search Handler
	void SetSearchContent(const CString& value);
#endif // SHARED_HANDLERS

	enum 
	{
		dispidregRemove = 20L,
		dispidregGetValue = 19L,
		dispidregSetValue = 18L,
		dispidGetRegisterUrl = 17L,
		dispidwsAddTag = 16L,
		dispidgetParam = 15L,
		dispidcloseDialog = 14L,
		dispidwsUpdateMemory = 13L,
		dispidwsGetTodayMemoryList = 12L,
		dispidwsDelTag = 11L,
		dispidwsUpdateTag = 10L,
		dispidwsDelRecord = 9L,
		dispidwsUpdateRecord = 8L,
		dispidwsAddRecord = 7L,
		dispidwsGetTagRecordList = 6L,
		dispidwsGetTagList = 5L,
		dispidwsLogin = 4L,
		dispidopenMainWin = 3L,
		dispidopenDialog = 2L,
		dispidtestMethod = 1L
	};

private:
	Cbdc2Dialog dialog;
	CString param;
	CString returnValue;

	UINT threadId;
	vector<WsInvokeInfo*> invokeList;

public:
	void OnLogin(UINT threadId, LONG64 userId);
	void OnGetTagList(UINT threadId, LPTSTR pResult);
	void OnGetTagRecordList(UINT threadId, LPTSTR pResult);
	void OnAddRecord(UINT threadId, BOOL success);
	void OnUpdateRecord(UINT threadId, BOOL success);
	void OnDelRecord(UINT threadId, BOOL success);
	void OnAddTag(UINT threadId, LONG64 tagId);
	void OnUpdateTag(UINT threadId, BOOL success);
	void OnDelTag(UINT threadId, BOOL success);
	void OnGetTodayMemoryList(UINT threadId, LPTSTR pResult);
	void OnUpdateMemory(UINT threadId, BOOL success);

protected:
	void			testMethod(void);

	BSTR			getRegisterUrl(void);
	BSTR			openDialog(LPCTSTR url, LPCTSTR title, LONG width, LONG height, LPCTSTR param);
	VARIANT_BOOL	closeDialog(LPCTSTR returnValue);
	BSTR			getParam(void);
	void			openMainWin(void);

	void wsLogin(LPCTSTR mail, LPCTSTR password, IDispatch* jsMethod);
	void wsGetTagList(IDispatch* jsMethod);
	void wsGetTagRecordList(LONGLONG tagId, LONGLONG from, LONGLONG count, IDispatch* jsMethod);
	void wsAddRecord(LPCTSTR memory, LPCTSTR description, LPCTSTR tag, IDispatch* jsMethod);
	void wsUpdateRecord(LONGLONG recordId, LPCTSTR memory, LPCTSTR description, LPCTSTR tag, IDispatch* jsMethod);
	void wsDelRecord(LONGLONG recordId, IDispatch* jsMethod);
	void wsAddTag(LPCTSTR tagName, IDispatch* jsMethod);
	void wsUpdateTag(LONGLONG tagId, LPCTSTR tag, IDispatch* jsMethod);
	void wsDelTag(LONGLONG tagId, IDispatch* jsMethod);
	void wsGetTodayMemoryList(LPCTSTR tag, IDispatch* jsMethod);
	void wsUpdateMemory(LONGLONG recordId, LONG score, IDispatch* jsMethod);

private:
	void addInvokeInfo(UINT threadId, IDispatch* pDisp);
	void invokeJsMethod(UINT threadId, CString result);

	LPThreadParam			getThreadParam(void* methodParam);
	static LPRecordParam	getRecordParam(LONG64 recordId, LPCTSTR memory, LPCTSTR description, LPCTSTR tag);
	static void				delRecordParam(LPRecordParam pRecordParam);
	static LPTagParam		getTagParam(LONG64 tagId, LPCTSTR tag);
	static void				delTagParam(LPTagParam pRecordParam);

	static void sendViewMessage(UINT msg, WPARAM wParam = 0U, LPARAM lParam = 0L);

	static void wsLoginThread(void* param);
	static void wsGetTagListThread(void* param);
	static void wsGetTagRecordListThread(void* param);
	static void wsAddRecordThread(void* param);
	static void wsUpdateRecordThread(void* param);
	static void wsDelRecordThread(void* param);
	static void wsAddTagThread(void* param);
	static void wsUpdateTagThread(void* param);
	static void wsDelTagThread(void* param);
	static void wsGetTodayMemoryListThread(void* param);
	static void wsUpdateMemoryThread(void* param);
protected:
	void regSetValue(LPCTSTR name, LPCTSTR value);
	BSTR regGetValue(LPCTSTR name);
	void regRemove(LPCTSTR name);
};
